package com.mes.cloud.web;

import com.mes.cloud.api.dto.request.*;
import com.mes.cloud.api.dto.response.DictionaryAndModuleDto;
import com.mes.cloud.api.dto.response.SysDictDto;
import com.mes.cloud.api.dto.response.SysDictionaryDto;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.common.HttpReturnEnums;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.service.SysDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author zhengyangping
 * @version 2019-07-30 13:43:19
 * @email 407892370@qq.com
 */
@Api(tags = "字典表", value = "字典表接口")
@RestController
@RequestMapping(value = "/sysDictionary")
public class SysDictionaryController {

    private SysDictionaryService sysDictionaryService;

    @Autowired
    public SysDictionaryController(SysDictionaryService sysDictionaryService) {
        this.sysDictionaryService = sysDictionaryService;
    }

    /**
     * 描述 字典表分页查询
     *
     * @param {pageRequest}
     * @return com.shinho.tntframe.core.common.model.JsonResult<com.shinho.tntframe.core.common.model.CommPager       <       com.shinho.crm.dict.api.dto.response.SysDictionaryDto>>
     * @author 郑样平
     * @date 2019/8/2 19:10
     */
    @ApiOperation(value = "字典表分页查询", notes = "字典表分页查询")
    @PostMapping(value = "/listPage")
    public JsonResult<CommPager<SysDictionaryDto>> listPage(@Valid @RequestBody SysDictionaryPageRequest pageRequest) {
        CommPager<SysDictionaryDto> commPager = sysDictionaryService.selectPage(pageRequest);
        JsonResult<CommPager<SysDictionaryDto>> jsonResult = new JsonResult<>();
        return jsonResult.setCode(HttpReturnEnums.Success.value()).setData(commPager);
    }

    /**
     * RPC使用
     * 描述 字典表查询所有
     *
     * @param {sysDictionaryRequest}
     * @return com.shinho.tntframe.core.common.model.JsonResult<java.util.List       <       com.shinho.crm.dict.api.dto.response.SysDictionaryDto>>
     * @author 郑样平
     * @date 2019/8/2 19:10
     */
    @ApiOperation(value = "字典表查询所有", notes = "字典表查询所有")
    @PostMapping(value = "/list")
    public JsonResult<List<SysDictionaryDto>> list(@RequestBody SysDictionaryRequest sysDictionaryRequest) {
        List<SysDictionaryDto> list = sysDictionaryService.list(sysDictionaryRequest);
        JsonResult<List<SysDictionaryDto>> jsonResult = new JsonResult<>();
        return jsonResult.setCode(HttpReturnEnums.Success.value()).setData(list);
    }

    /**
     * 前端自己用的
     * 功能: 根据code查询数据
     *
     * @param code
     * @return
     */
    @ApiOperation(value = " 根据code查询数据", notes = " 根据code查询数据")
    @ApiImplicitParam(paramType = "String", name = "code", value = "字典编码", required = true)
    @GetMapping(value = "getListByCode/{code}")
    public JsonResult<List<SysDictDto>> getListByCode(@NotEmpty(message = "code 不能为空！") @PathVariable String code) {
        List<SysDictDto> list = sysDictionaryService.getListByCode(code);
        JsonResult<List<SysDictDto>> jsonResult = new JsonResult<>();
        return jsonResult.setCode(HttpReturnEnums.Success.value()).setData(list);
    }

    /**
     * 描述 字典表新增
     *
     * @param {sysDictionaryRequest}
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author 郑样平
     * @date 2019/8/3 8:44
     */
    @ApiOperation(value = "字典表新增", notes = "字典表新增")
    @PostMapping(value = "/add")
    public JsonResult add(@Valid @RequestBody SysDictionaryAddRequest sysDictionaryAddRequest) {
        return sysDictionaryService.insertSelective(sysDictionaryAddRequest);
    }

    /**
     * 描述 字典表修改
     *
     * @param {sysDictionaryRequest}
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author 郑样平
     * @date 2019/8/3 8:45
     */
    @ApiOperation(value = "字典表修改", notes = "字典表修改")
    @PostMapping(value = "/update")
    public JsonResult update(@Valid @RequestBody SysDictionaryUpdateRequest sysDictionaryUpdateRequest) {
        return sysDictionaryService.updateSelectiveById(sysDictionaryUpdateRequest);
    }

    /**
     * 描述 字典表批量禁用启用
     *
     * @param {ids]
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author 郑样平
     * @date 2019/8/3 8:46
     */
    @ApiOperation(value = "字典表批量禁用启用", notes = "字典表批量禁用启用")
    @PostMapping(value = "/batchEnable")
    public JsonResult batchEnable(@Valid @RequestBody SysDicibatchEnableRequest sysDicibatchEnableRequest) {
        return sysDictionaryService.batchEnable(sysDicibatchEnableRequest);
    }

    /**
     * 描述 字典表查看详情
     *
     * @param {id}
     * @return com.shinho.tntframe.core.common.model.JsonResult<com.shinho.crm.dict.api.dto.response.SysDictionaryDto>
     * @author 郑样平
     * @date 2019/8/3 8:46
     */
    @ApiOperation(value = "字典表查看详情", notes = "字典表查看详情")
    @ApiImplicitParam(paramType = "String", name = "id", value = "字典表主键", required = true)
    @GetMapping(value = "get/{id}")
    public JsonResult<SysDictionaryDto> get(@NotEmpty(message = "id 不能为空！") @PathVariable String id) {
        return sysDictionaryService.get(id);
    }

    /**
     * 描述 模板字典组合树
     *
     * @param
     * @return com.shinho.tntframe.core.common.model.JsonResult<java.util.List       <       com.shinho.crm.dict.api.dto.response.DictionaryAndModuleDto>>
     * @author 郑样平
     * @date 2019/8/3 8:47
     */
    @ApiOperation(value = "模板字典组合树", notes = "模板字典组合树")
    @PostMapping(value = "/treeList")
    public JsonResult<List<DictionaryAndModuleDto>> treeList(@RequestBody TreeLIstRequest treeLIstRequest) {
        List<DictionaryAndModuleDto> list = sysDictionaryService.treeList(treeLIstRequest);
        JsonResult<List<DictionaryAndModuleDto>> jsonResult = new JsonResult<>();
        return jsonResult.setCode(HttpReturnEnums.Success.value()).setData(list);
    }

    @ApiOperation(value = "是否默认", notes = "是否默认")
    @PostMapping(value = "/updateDefaultFlag")
    public JsonResult updateDefaultFlag(@Valid @RequestBody SysDictionaryDefaultFlagRequest sysDictionaryDefaultFlagRequest) {
        return sysDictionaryService.updateDefaultFlag(sysDictionaryDefaultFlagRequest);
    }

    /**
     * 描述 字典表查询所有
     *
     * @param {sysDictionaryRequest}
     * @return com.shinho.tntframe.core.common.model.JsonResult<java.util.List       <       com.shinho.crm.dict.api.dto.response.SysDictionaryDto>>
     * @author 郑样平
     * @date 2019/8/2 19:10
     */
    @ApiOperation(value = "根据编码和值返回下级", notes = "根据编码和值返回下级")
    @PostMapping(value = "/codeAndValueList")
    public JsonResult<List<SysDictionaryDto>> codeAndValueList(@RequestBody CodeAndValueRequest codeAndValueRequest) {
        return sysDictionaryService.codeAndValueList(codeAndValueRequest);
    }

    /**
     * 描述 根据编码和显示值模糊获取列表
     *
     * @param {sysDictionaryRequest}
     * @return com.shinho.tntframe.core.common.model.JsonResult<java.util.List       <       com.shinho.crm.dict.api.dto.response.SysDictionaryDto>>
     * @author 郑样平
     * @date 2019/8/2 19:10
     */
    @ApiOperation(value = "根据编码和显示值获取列表", notes = "根据编码和显示值获取列表")
    @PostMapping(value = "/codeAndNameList")
    public JsonResult<List<SysDictionaryDto>> codeAndNameList(@RequestBody CodeAndNameListRequest codeAndNameListRequest) {
        List<SysDictionaryDto> sysDictionaryDtos = sysDictionaryService.codeAndNameList(codeAndNameListRequest);
        JsonResult<List<SysDictionaryDto>> jsonResult = new JsonResult<>();
        return jsonResult.setCode(HttpReturnEnums.Success.value()).setData(sysDictionaryDtos);
    }

    @ApiOperation(value = "根据编码list", notes = "根据编码list")
    @PostMapping(value = "/findListByCodes")
    public JsonResult<List<SysDictionaryDto>> findListByCodes(@Valid @RequestBody List<String> codes) {
        return sysDictionaryService.findListByCodes(codes);
    }
}
